#-*- coding:utf-8 -*-
#!/usr/bin/env python3

'''
    【简介】
	PyQT5中 QTreeWidget 例子
   
  
'''

from PyQt5.QtWidgets import *
import sys

class TreeWidgetDemo(QMainWindow):   
	def __init__(self,parent=None):
		super(TreeWidgetDemo,self).__init__(parent)
		self.setWindowTitle('TreeWidget 例子')
		self.tree = QTreeWidget()
        # 设置列数
		self.tree.setColumnCount(2)
        # 设置头的标题
		self.tree.setHeaderLabels(['Key','Value'])
		with open('mulu.csv','r',encoding='u8') as f:
			mulu=f.read()
			self.dict1=eval(mulu)
		for k,v in self.dict1.items():
			if len(k) == 1:
				root = QTreeWidgetItem(self.tree)
				root.setText(0, v)
				root.setText(1, k)
			elif len(k) == 3:
				child = QTreeWidgetItem(root)
				child.setText(0, v[0])
				child.setText(1, k)
			elif len(k) == 5:
				if v!='选择查询模板':
					child1 = QTreeWidgetItem(child)
					child1.setText(0, v)
					child1.setText(1, k)

		# root= QTreeWidgetItem(self.tree)
		# root.setText(0,'企业一套表统计调查制度(2021)')
		# root.setText(1,'0')
		#
		# child1 = QTreeWidgetItem(root)
		# child1.setText(0,'月报')
		# child1.setText(1,'1')
		#
		# child2 = QTreeWidgetItem(root)
		# child2.setText(0,'季报')
		# child2.setText(1,'2')
		#
		# child3 = QTreeWidgetItem(root)
		# child3.setText(0,'年报')
		# child3.setText(1,'3')
		#
		# child4 = QTreeWidgetItem(child3)
		# child4.setText(0,'财务状况')
		# child4.setText(1,'301')
		#
		# child5 = QTreeWidgetItem(child3)
		# child5.setText(0,'劳动工资')
		# child5.setText(1,'302')
		#
		# child6 = QTreeWidgetItem(child5)
		# child6.setText(0,'查询1')
		# child6.setText(1,'30101')
		#
		# child7 = QTreeWidgetItem(child5)
		# child7.setText(0, '查询2')
		# child7.setText(1, '30102')

        
		self.tree.addTopLevelItem(root)
		self.tree.clicked.connect( self.onTreeClicked )
        		
		self.setCentralWidget(self.tree)  

	def onTreeClicked(self, qmodelindex):
		item = self.tree.currentItem()
		print("key=%s ,value=%s" % (item.text(0), item.text(1)))
		#截取item.text(1)前三位,得到报表
		#截取item.text(1)第一位,得到周期
		if len(item.text(1))==5:
			zdcode=item.text(1)[:1]
			bbcode=item.text(1)[:3]
			print(type(bbcode))
			print(self.dict1['23'])
			print(self.dict1[zdcode])
			print(self.dict1[bbcode])
			print(self.dict1[bbcode][0])
			print(self.dict1[bbcode][1])



        
if __name__ == '__main__':
	app = QApplication(sys.argv)
	tree = TreeWidgetDemo()
	tree.show()
	sys.exit(app.exec_())
